{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6da9f0ae-bcee-43b7-8c17-d2d7c65e7085",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<>:31: SyntaxWarning: invalid escape sequence '\\s'\n",
      "<>:31: SyntaxWarning: invalid escape sequence '\\s'\n",
      "/tmp/ipykernel_2490/960662833.py:31: SyntaxWarning: invalid escape sequence '\\s'\n",
      "  plt.title('Time Domain: $f(x) = A \\sin(n x) + B \\sin(m x)$')\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d8009418a3a84a67a0e1b46ec3b6144a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(FloatSlider(value=1.0, description='A:', max=3.0, min=0.1), FloatSlider(value=1.0, descr…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from ipywidgets import interact, FloatSlider\n",
    "\n",
    "# 初始化参数和采样设置\n",
    "N = 1000  # 采样点数\n",
    "x = np.linspace(0, 4 * np.pi, N)  # x 范围 [0, 4π]\n",
    "\n",
    "# 定义信号生成函数\n",
    "def generate_signal(A, n, B, m):\n",
    "    return A * np.sin(n * x) + B * np.sin(m * x)\n",
    "\n",
    "# 计算傅里叶变换\n",
    "def compute_fft(signal):\n",
    "    fft_result = np.fft.fft(signal)  # 快速傅里叶变换\n",
    "    magnitude = np.abs(fft_result[:N//2])  # 取前半部分（对称性）\n",
    "    frequency = np.fft.fftfreq(N, d=(x[1]-x[0]))[:N//2]  # 频率轴\n",
    "    return frequency, magnitude\n",
    "\n",
    "# 动态绘图函数\n",
    "def plot_signal(A=1.0, n=1.0, B=0.5, m=3.0):\n",
    "    signal = generate_signal(A, n, B, m)\n",
    "    freq, mag = compute_fft(signal)\n",
    "    \n",
    "    # 创建画布\n",
    "    plt.figure(figsize=(12, 6))\n",
    "    \n",
    "    # 时域图\n",
    "    plt.subplot(1, 2, 1)\n",
    "    plt.plot(x, signal, 'b-', linewidth=2)\n",
    "    plt.title('Time Domain: $f(x) = A \\sin(n x) + B \\sin(m x)$')\n",
    "    plt.xlabel('x')\n",
    "    plt.ylabel('Amplitude')\n",
    "    plt.grid(True)\n",
    "    \n",
    "    # 频域图（频谱）\n",
    "    plt.subplot(1, 2, 2)\n",
    "    plt.plot(freq, mag, 'r-', linewidth=2)\n",
    "    plt.title('Frequency Domain (FFT)')\n",
    "    plt.xlabel('Frequency (Hz)')\n",
    "    plt.ylabel('Magnitude')\n",
    "    plt.grid(True)\n",
    "    plt.tight_layout()\n",
    "    plt.show()\n",
    "\n",
    "# 创建交互控件\n",
    "interact(\n",
    "    plot_signal,\n",
    "    A=FloatSlider(min=0.1, max=3.0, step=0.1, value=1.0, description='A:'),\n",
    "    n=FloatSlider(min=0.5, max=5.0, step=0.5, value=1.0, description='n:'),\n",
    "    B=FloatSlider(min=0.1, max=3.0, step=0.1, value=0.5, description='B:'),\n",
    "    m=FloatSlider(min=0.5, max=10.0, step=0.5, value=3.0, description='m:')\n",
    ");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc4cc882-1747-465d-817f-dab7d975c4f6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
